<template>
  <el-form ref="form" :model="form" label-width="80px">
    <el-form-item label="商品名称">
      <el-input v-model="form.name" placeholder="商品名称"></el-input>
    </el-form-item>
    <!-- <el-form-item label="商品类型">
      <el-cascader
        v-model="value"
        :options="options"
        @change="handleChange"
        style="width: 500px"
        placeholder="请选择商品的类型"
        @click="handleChangeaa()"
      ></el-cascader>
    </el-form-item> -->

    <el-form-item label="商品类型">
      <div>
        <el-radio-group
          v-model="form.resourcesp"
          v-for="item in this.subclassifyandClassify"
          :key="item.id"
        >
          <el-radio
            :label="item.id"
            :values="item.id"
            @change="shangpinleixing(item.id)"
            >{{ item.classifyname }}</el-radio
          >
        </el-radio-group>
      </div>
    </el-form-item>
    <el-form-item label="商品分类">
      <div>
        <el-radio-group
          v-model="form.resource"
          v-for="item in this.subclassifyandClassifyss"
          :key="item.id"
        >
          <el-radio :label="item.subclassifyfyname">{{
            item.subclassifyfyname
          }}</el-radio>
        </el-radio-group>
      </div>
    </el-form-item>
    <el-form-item label="面料">
      <el-radio-group v-model="form.resourcegg">
        <el-radio label="纯棉"></el-radio>
        <el-radio label="人造革"></el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="商品价格">
      <el-input v-model="form.Price" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" plain>主要按钮</el-button>
    </el-form-item>

    <el-form-item label="商品图片">
      <el-select
        v-model="value1"
        multiple
        placeholder="请从图库列表中选择商品需要的图片id"
        style="width: 500px"
        @change="Change"
      >
        <el-option
          v-for="item in fileList"
          :key="item.id"
          :label="item.id"
          :value="item.id"
        >
        </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="图库列表">
      <ul class="infinite-list" style="overflow: auto">
        <el-upload
          class="upload-demo"
          :action="uploadActionUrl"
          :on-preview="handlePreview"
          :file-list="fileList"
          list-type="picture"
        ></el-upload>
      </ul>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">立即创建</el-button>
      <el-button native-type="reset">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { GetImage } from "../api/files";
import { getsubclassifyandClassify,getid } from "../api/home";
export default {
  data() {
    return {
      form: {
        name: "",
        Price: "",
        resource: "",
        resourcegg:"",
        resourcesp: "",
      },
      values: "",
      input: "",
      value: [],
      //图片数组
      fileList: [],
      drawer: false,
      optionss: [],
      options: [
        {
          value: "",
          label: "",
          children: [
            {
              value: "",
              label: "",
            },
          ],
        },
      ],

      value1: [],
      subclassifyandClassify: [],
      subclassifyandClassifyss: [],
    };
  },
  methods: {
    shangpinleixing(id){
      console.log(id);
      getid(id).then((data)=>{
        console.log(data);
        this.subclassifyandClassifyss = data.data.data
        console.log(this.subclassifyandClassifyss);
      })
    },
    onSubmit() {
      console.log(this.form.name);
      console.log(this.form.resource);
      console.log(this.form.Price);
      console.log(this.value);
      console.log(this.value1);
      console.log(this.resourcesp);
          console.log(this.values);
    },
    handleChangeaa() {},
    handleChange(label) {
      console.log(label);
    },
    Change() {
      console.log(this.value1);
    },
    handlePreview(file) {
      console.log(file);
      console.log(file.indexOf);
      // this.fileList.splice(file.indexOf,1)
    },

    fetchData() {
      GetImage()
        .then((data) => {
          var res = data.data;
          console.log(res);
          for (var index = 0; index < res.length; index++) {
            var obj = {
              indexOf: index,
              id: res[index].id,
              name: res[index].id,
              url: "http://localhost:5000/files/" + res[index].id,
            };

            this.fileList.push(obj);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
  mounted() {
    getsubclassifyandClassify().then((data) => {
      console.log(data.data.data);
      var res = data.data.data;
      console.log(res);
      res.data.forEach((element) => {
        this.subclassifyandClassify.push(element);
      });
    });
    this.fetchData();
    console.log(this.fileList);

  },

  computed: {
    uploadActionUrl() {
      const baseUrl = "http://localhost:5000/";
      const uploadUrl = "files/uploadfiles";
      return baseUrl + uploadUrl;
    },
  },
};
</script>
<style scoped>
.upload-demo {
  max-height: 300px;
}
</style>